package com.tanhua.dubbo.server.controller;

import com.tanhua.common.utils.NoAuthorization;
import com.tanhua.dubbo.server.service.DashboardService;
import com.tanhua.dubbo.server.vo.DistributionVo;
import com.tanhua.dubbo.server.vo.SummaryVo;
import com.tanhua.dubbo.server.vo.UsersVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("dashboard")
@Slf4j
public class DashboardController {

    @Autowired
    private DashboardService dashboardService;

    /**
     * 注册用户分布，行业top、年龄、性别、地区
     *
     * 开始时间
     * @param sd
     * 结束时间
     * @param ed
     * @return
     */
    @GetMapping("distribution")
    @NoAuthorization
    public ResponseEntity distribution(String sd, String ed){
        try {
            //根据开始时间和结束时间来查询这段时间内所有的信息
            DistributionVo allUserInfo = dashboardService.queryAllUserInfo(sd,ed);
            //返回页面数据
            return ResponseEntity.ok(allUserInfo);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询有误");
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("查询有误");
    }

    /**
     * 新增、活跃用户、次日留存率
     * @param sd
     * @param ed
     * @param type
     * @return
     */
    @GetMapping("users")
    public ResponseEntity users(Long sd, Long ed, Integer type){
        try {
            UsersVo usersInfo = dashboardService.queryUsersInfo(sd,ed,type);
            return ResponseEntity.ok(usersInfo);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询有误");
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("查询有误");
    }

    /**
     * 概要信息统计
     * @return
     */
    @GetMapping("summary")
    public ResponseEntity summary(){
        try {
            SummaryVo summaryVo = dashboardService.querySummary();
            return ResponseEntity.ok(summaryVo);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询有误");
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("查询有误");
    }
}
